<template>
  <div class="Download">
   
  </div>
</template>

<script>
export default {
  name: "DownLoad",
  props: {
    //   获取音乐ID
    uid: {
      type: Array,
      defalut: [],
    },
  },
  data() {
    return {};
  },
  methods: {
    open2() {
      this.$notify({
        title: "下载中",
        message: "下载中...",
        position: "bottom-right",
        type: "info",
      });
    },
    open3() {
      this.$notify({
        title: "下载",
        message: "下载已完成！",
        position: "bottom-right",
        type: "success",
        duration: 1500,
      });
    },
    // 下载音乐
    downLoad(uid,MusicName) {
      this.open2();
      this.axios({
        method: "GET",
        url: "https://autumnfish.cn/song/url?id=" + uid,
      })
        .then((res) => {
          // console.log(res.data.data[0].url);
          // console.log(this.musicDownLoadName);
          if (res.data.data[0]) {
            fetch(res.data.data[0].url)
              .then((res) => res.blob())
              .then((blob) => {
                const a = document.createElement("a");
                document.body.appendChild(a);
                a.style.display = "none";
                // 使用获取到的blob对象创建的url
                const url = window.URL.createObjectURL(blob);
                a.href = url;
                // 指定下载的文件名
                a.download = MusicName + ".mp3";
                a.click();
                document.body.removeChild(a);
                // 移除blob对象的url
                window.URL.revokeObjectURL(url);
                this.open3();
              });
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>
